//
//  BaseVC.m
//  itboyeios_gd
//
//  Created by Apple on 15/10/31.
//  Copyright © 2015年 Boye. All rights reserved.
//

#import "BaseVC.h"
#import "BaseNavVC.h"

@interface BaseVC ()
{
    UIButton *_finishButton;
}
@end

@implementation BaseVC

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // 键盘弹出通知
    [self addKeyboardNotification];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self isNaviTranslucent:YES statesLightContent:YES];
    // 标题白色
    NSDictionary *attr = @{NSForegroundColorAttributeName:[UIColor whiteColor],
                           NSFontAttributeName:[UIFont systemFontOfSize:18]};
    [self.navigationController.navigationBar setTitleTextAttributes:attr];
}

//
/**
 *  导航栏样式设置，有透明和绿色两种
 *
 *  @param translucent 导航栏样式有透明和绿色两种
 *  @param light       状态栏颜色，YES则为白色，NO为黑色
 */
- (void)isNaviTranslucent:(BOOL)translucent statesLightContent:(BOOL)light {
    
    if (translucent) {
        if (!light) {
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        } else {
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        }
        self.navigationController.navigationBar.translucent = YES;
        self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
    } else {
        if (!light) {
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        } else {
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        }
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
//        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"导航栏"] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:JSQ_COLOR] forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
    }
}

#pragma mark - 导航栏左边返回按钮
// 创建导航栏左边返回按钮
- (void)createLeftBarButtonItem
{
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25/2.0, 43/2.0)];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"返回白"] forState:UIControlStateNormal];
    UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backBarButton;
}

// 创建导航栏右侧的保存按钮
- (void)createRightBarButtonItem {
    UIButton *saveButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveButton addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *saveBarButton = [[UIBarButtonItem alloc] initWithCustomView:saveButton];
    self.navigationItem.rightBarButtonItem = saveBarButton;
}
// 返回上一视图控制器
- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
// 保存事件
- (void)saveAction
{
}


#pragma mark - 键盘弹出和下降相关设置
// 创建编辑完成后使键盘下降的按钮
- (void)_createFinishEditButton
{
    _finishButton = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 30)];
    _finishButton.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    _finishButton.layer.borderWidth = 0.5;
    _finishButton.layer.borderColor = [UIColor grayColor].CGColor;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-60, 0, 60, 30)];
    titleLabel.text = @"完成";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = BLUE_COLOR;
    [_finishButton addSubview:titleLabel];
    [_finishButton addTarget:self action:@selector(keyboardDown) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_finishButton];
}


// 键盘弹出和下降通知
- (void)addKeyboardNotification
{
    // 键盘弹出通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillShowNotification object:nil];
    // 键盘下降通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillHideNotification object:nil];
}



// 键盘弹出时执行，“完成”按钮跟随键盘一起上升
- (void)keyboardFrameChange:(NSNotification *)notification
{
    if (_finishButton == nil) {
        // 创建编辑完成后使键盘下降的按钮
        [self _createFinishEditButton];
    }
    [UIView animateWithDuration:0.15 animations:^{
        //获取键盘的frame
        NSValue *boundsValue = [notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect frame = [boundsValue CGRectValue];
        //键盘y值
        CGFloat y = frame.origin.y;
        // "完成"按钮下降
        if (y < SCREEN_HEIGHT-64) {
            if (self.navigationController.navigationBar.translucent) {
                _finishButton.bottom = y;
            }
            else {
                _finishButton.bottom = y - 64;
            }
        }
        else {
            _finishButton.top = y;
        }
    }];
}



// 编辑完成按钮点击事件处理
- (void)keyboardDown
{
    // 使键盘下降
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}



@end
